package com.xquant.marketdata.curvestrip.entity;

import com.xquant.marketdata.utils.DateUtils;
import com.xquant.marketdata.utils.ExcelAttribute;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.*;

/**
 * @Author: xpp -_- -_-
 * @Date: 2021/3/22 16:10
 * 违约率报价导入  上海清算所模板实体类  从16行开始取信息
 */
public class TdailyQuoteClean implements IDailyQuote  {
    public final static int MODEL_TYPE = 1;

    public final static List titleList = Arrays.asList("序号","参考实体（全称）","参考实体上海清算所持有人代码","评级","行业","期限（年）","卖出保护报价（bp）","买入保护报价（bp）");

    @ExcelAttribute(name = "参考实体（全称）")
    private String iName;

    @ExcelAttribute(name = "参考实体上海清算所持有人代码")
    private String iCode;

    //评级
    @ExcelAttribute(name = "评级")
    private String level;

    //行业
    @ExcelAttribute(name = "行业")
    private String industry;

    //期限 单位：年
    @ExcelAttribute(name = "期限（年）")
    private String term;

    //报卖价
    @ExcelAttribute(name = "卖出保护报价（bp）")
    private String ask;

    //报买价
    @ExcelAttribute(name = "买入保护报价（bp）")
    private String bid;

    //报卖价
    private BigDecimal dpAsk;

    //报买价
    private BigDecimal dpBid;
    //中间价
    private BigDecimal dpMid;

    private String begDate;

    public String getiName() {
        return iName;
    }

    public void setiName(String iName) {
        this.iName = iName;
    }

    public String getiCode() {
        return iCode;
    }

    public void setiCode(String iCode) {
        this.iCode = iCode;
    }

    public String getLevel() {
        return level;
    }

    public void setLevel(String level) {
        this.level = level;
    }

    public String getIndustry() {
        return industry;
    }

    public void setIndustry(String industry) {
        this.industry = industry;
    }

    public String getTerm() {
        return term;
    }

    public String getResolveTerm() {
        switch (term){
            case "0.5":
                return "6M";
            default:
                return term + "Y";
        }
    }

    public void setTerm(String term) {
        this.term = term;
    }

    public String getAsk() {
        return ask;
    }

    public void setAsk(String ask) {
        this.ask = ask;
    }

    public String getBid() {
        return bid;
    }

    public void setBid(String bid) {
        this.bid = bid;
    }

    public BigDecimal getDpAsk() {
        if(dpAsk != null) {
            return dpAsk;
        }
        dpAsk = StringUtils.isBlank(this.ask)?BigDecimal.ZERO:new BigDecimal(this.ask).divide(new BigDecimal("10000"));
        return dpAsk;
    }

    public void setDpAsk(  BigDecimal dpAsk) {
        this.dpAsk = dpAsk;
    }

    public   BigDecimal getDpBid() {
        if(dpBid != null) {
            return dpBid;
        }
        dpBid = StringUtils.isBlank(this.bid)?BigDecimal.ZERO:new BigDecimal(this.bid).divide(new BigDecimal("10000"));
        return dpBid;
    }

    public void setDpBid(  BigDecimal dpBid) {
        this.dpBid = dpBid;
    }

    public BigDecimal getDpMid() {
        if(dpMid != null) {
            return dpMid;
        }
        dpMid = getDpAsk().add(getDpBid()).divide(new BigDecimal("2"));
        return dpMid;
    }

    public void setDpMid(  BigDecimal dpMid) {
        this.dpMid = dpMid;
    }

    public String getBegDate() {
        return begDate;
    }

    public void setBegDate(String begDate) {
        this.begDate = begDate;
    }

    public TdailyQuoteClean() {
    }

    @Override
    public String toString() {
        return "tdailyQuoteClean{" +
                "iName='" + iName + '\'' +
                ", iCode='" + iCode + '\'' +
                ", level='" + level + '\'' +
                ", industry='" + industry + '\'' +
                ", term='" + term + '\'' +
                ", dpAsk=" + getDpAsk() +
                ", dpBid=" + getDpBid() +
                ", dpMid=" + dpMid +
                '}';
    }


    public static List<TdailyQuoteClean> resolveCleans(List<TdailyQuoteClean> datas,String begDate){
        //去除重复数据，以最后一条为准
        Map<String, TdailyQuoteClean> map = new WeakHashMap<>();
        begDate = DateUtils.getFormatDate(begDate);
        //处理begDate格式为yyyy-mm-dd
        for (TdailyQuoteClean clean : datas){
            clean.setBegDate(begDate);
            map.put(clean.getiCode()+"_"+clean.getTerm(), clean);
        }
        return new ArrayList<TdailyQuoteClean>(map.values());
    }

}

